package org.example.mybatisplus.config;

import org.example.mybatisplus.dao.entity.SysUser;
import org.example.mybatisplus.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SecurityService implements UserDetailsService{

   @Autowired
    private ISysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userService.lambdaQuery().eq(SysUser::getName, username).one();
        if(user != null){
            List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
            simpleGrantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            //BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            //String result = encoder.encode("123456");
            return new User(user.getName(), user.getPwd(), simpleGrantedAuthorities);
        }
        return null;
    }


}
